package io.renren.modules.app.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import io.renren.modules.app.service.DayEndSettleService;
import io.renren.modules.app.service.QuartzDayEndSettleService;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class QuartzDayEndSettleServiceImpl implements QuartzDayEndSettleService {

	//时间间隔
	private final long TIME_INTERVAL = 3000;
	
	//循环次数
	private final Integer LOOP_COUNT = 6;
	
	@Autowired
	private DayEndSettleService dayEndSettleService;
	
	/**
	 * 每日定时执行对账任务
	 * 暂定,如果对账失败(发送结果文件失败),重试6次,每10分钟 1次
	 */
	@Override
	public void execute() {
		boolean isSucc = false;
		for (int i = 0; i < LOOP_COUNT; i++) {
			log.error("第{}次日终对账开始........................." , i + 1);
			try {
				isSucc = dayEndSettleService.DayOfEndSettle(null);
			} catch (Exception e) {
				// TODO: handle exception
				log.error("对账失败!" , e);
			}
			log.error("第{}次日终对账结束, 对账结果:{}........................." , i + 1, isSucc);
			if(!isSucc) {
				try {
					Thread.sleep(TIME_INTERVAL);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					log.error("" , e);
				}
			}else {
				return;
			}
		}
		
		
	}

}
